package org.jeecg.modules.innerApi.leqiApi.convert;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.innerApi.common.responseDto.InvoiceRtn;
import org.jeecg.modules.innerApi.leqiApi.responseVo.LQApiResponse;

@Slf4j
public class LQResponseRtnConvert {

    /**
     * 发票开具返回转换
     * @param response
     * @return
     */
    public static InvoiceRtn fpkjRtnConvert(LQApiResponse response){
        InvoiceRtn rtn=new InvoiceRtn();
        LQApiResponse.Response responseObject = response.getResponse();
        if(responseObject != null && responseObject.getError() != null) {
            //返回失败信息
            LQApiResponse.ResponseError error = responseObject.getError();
            rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500.toString());
            rtn.setSuccess(false);
            rtn.setMsg(error.getMessage());
        } else if(responseObject != null && responseObject.getData() != null) {
            String data = responseObject.getData();
            JSONObject dataObject = JSON.parseObject(data);
            String returncode = dataObject.getString("returncode");
            String returnmsg = dataObject.getString("returnmsg");
            if("00".equals(returncode)){
                //成功
                String fpdm = dataObject.getString("fpdm");
                String fphm = dataObject.getString("fphm");
                String kprq = dataObject.getString("kprq");

                rtn.setCode(CommonConstant.SC_OK_200.toString());
                rtn.setSuccess(true);
                rtn.setMsg(returnmsg);

                InvoiceRtn.InvoiceRtnData rtnData = new InvoiceRtn.InvoiceRtnData();
                rtnData.setFpdm(fpdm);
                rtnData.setFphm(fphm);
                rtnData.setKprq(DateUtil.parse(kprq));

                rtn.setData(rtnData);
            } else {
                //失败
                rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500.toString());
                rtn.setSuccess(false);
                rtn.setMsg(returnmsg);
            }
        } else {
            rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500.toString());
            rtn.setSuccess(false);
            rtn.setMsg("返回数据格式错误");
        }
        return rtn;
    }


}
